﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Broadsides2d
{
    public class Cannonball
    {
        public static Texture2D cannonballImage;

        private float heading;
        private float x;
        private float y;

        private int life;
        private int range;

        private const float scale = .02f;
        private int damage = 10;

        private Object owner;
        const int speed = 5;

        public Cannonball(float x, float y, float heading, Object owner)
        {
            this.x = x;
            this.y = y;
            this.heading = heading;
            this.owner = owner;
            this.range = life = 40;
            this.damage = 10;
        }
        public Cannonball(float x, float y, float heading, int range, int damage, Object owner)
        {
            this.x = x;
            this.y = y;
            this.heading = heading;
            this.owner = owner;
            this.range = life = range;
            this.damage = damage;
        }

        public void Update()
        {
            moveBall();
            checkCollide();
        }        

        public void Draw(SpriteBatch batch)
        {
            Texture2D texture = cannonballImage;
            Vector2 v = Game1.recenter(x, y);
            v.X = (v.X + World.world.worldSizeInPixels) % World.world.worldSizeInPixels;
            v.Y = (v.Y + World.world.worldSizeInPixels) % World.world.worldSizeInPixels;
            batch.Draw(texture, v, null, Color.White, heading, new Vector2(texture.Width / 2, texture.Height / 2), .02f, SpriteEffects.None, 0.0f);
        }

        private void moveBall()
        {
            clearTiles();

            x = (x + (float)Math.Sin(heading) * speed + World.world.worldSizeInPixels) % World.world.worldSizeInPixels;
            y = (y + (float)Math.Cos(heading) * speed * -1 + World.world.worldSizeInPixels) % World.world.worldSizeInPixels;

            if (owner is Ship)
            {
                x += (float)Math.Sin(((Ship)owner).getRotation()) * ((Ship)owner).getSpeed();
                y -= (float)Math.Cos(((Ship)owner).getRotation()) * ((Ship)owner).getSpeed();
            }

            for (int i = -1; i < 1; i++)
                for (int j = -1; j < 1; j++)
                    World.world.tiles[((int)((x / 8) + i) + World.world.worldSize) % World.world.worldSize, ((int)((y / 8) + j) + World.world.worldSize) % World.world.worldSize].contents.Add(this);

            life--;
            if (life <= 0)
            {
                clearTiles();
                Game1.cannonballs.Remove(this);
            }

        }
        public void checkCollide()
        {
            for (int i = -1; i < 1; i++)
                for (int j = -1; j < 1; j++)
                {
                    Tile t = World.world.tiles[((int)((x / 8) + i) + World.world.worldSize) % World.world.worldSize, ((int)((y / 8) + j) + World.world.worldSize) % World.world.worldSize];
                    for(int k = t.contents.Count - 1; k >=0; k--)
                    {
                        if (this.life > 0)
                        {
                            if (t.contents.ElementAt(k) is NPC && owner is Player)
                            {
                                NPC n = (NPC)(t.contents.ElementAt(k));
                                n.addLife(-1 * damage);
                                this.life = 0;
                                if (n.getLife() <= 0)
                                {
                                    n.explodeShip();
                                    ((Ship)owner).addGold(n.getGold());
                                }
                            }
                            else if (t.contents.ElementAt(k) is Player && (owner is Port || owner is AttackNPC))
                            {
                                Player p = (Player)(t.contents.ElementAt(k));
                                p.addLife(-1 * damage);
                                this.life = 0;
                                if (p.getLife() <= 0)
                                {
                                    //GAME OVER YEAAAAH
                                }
                            }
                        }
                    }
                }
        }

        public void clearTiles()
        {
            for (int i = -1; i < 1; i++)
                for (int j = -1; j < 1; j++)
                    if (World.world.tiles[((int)((x / 8) + i) + World.world.worldSize) % World.world.worldSize, ((int)((y / 8) + j) + World.world.worldSize) % World.world.worldSize].contents.Contains(this))
                        World.world.tiles[((int)((x / 8) + i) + World.world.worldSize) % World.world.worldSize, ((int)((y / 8) + j) + World.world.worldSize) % World.world.worldSize].contents.Remove(this);
        }

        public void setRange(int range)
        {
            this.range = range;
        }
        public float getRange()
        {
            return this.range;
        }
        public void setLife(int life)
        {
            this.life = life;
        }
        public float getLife()
        {
            return this.life;
        }

    }
}
